package com.wswy.wyjk.api.entity;

import android.text.TextUtils;

import com.blankj.utilcode.util.SPUtils;
import com.tencent.mmkv.MMKV;
import com.wswy.wyjk.api.MMKVConstant;
import com.wswy.wyjk.ui.uitls.ChannelManager;
import com.wswy.wyjk.BuildConfig;
import com.wswy.wyjk.api.Constant;
import com.wswy.wyjk.ui.uitls.ScUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class RequestParameter {
    private Map<String, String> mParams = createMap();

    public static RequestParameter generate() {
        return new RequestParameter();
    }

    private Map<String, String> createMap() {
        Map<String, String> treeMap = new HashMap<>();
        treeMap.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
        return treeMap;
    }

    public Map<String, String> getParams() {
        mParams.put("system", "android");
        mParams.put("platform", "app");
        mParams.put("channel", ChannelManager.getChannel());
        mParams.put("versionCode", String.valueOf(BuildConfig.VERSION_CODE));
        mParams.put("appId", Constant.APPID);
        if(!TextUtils.isEmpty(SPUtils.getInstance().getString(Constant.SP_UUID_KEY))) {
            mParams.put("uuid", SPUtils.getInstance().getString(Constant.SP_UUID_KEY));
        }else{
            mParams.put("uuid", "9725dcbd-7453-3092-aae3-35966d49e42d");
        }
        String token = MMKV.defaultMMKV().decodeString(MMKVConstant.TOKEN);
        if (!TextUtils.isEmpty(token)) {
            mParams.put("token", token);
        }
        return mParams;
    }

    public Map<String, String> getUUIDParams() {
        return mParams;
    }


    public Map<String, String> getSingedParams() {
        TreeMap<String, String> map = new TreeMap<>(getParams());
        String sign = ScUtil.ucSign(map);
        map.put("sign", sign);
        return map;
    }

    public Map<String, String> getSingedParams(Map<String, String> businessMap) {
        TreeMap<String, String> map = new TreeMap<>(getParams());
        if (businessMap != null) {
            map.putAll(businessMap);
        }
        String sign = ScUtil.ucSign(map);
        map.put("sign", sign);
        return map;
    }


    public RequestParameter addAll(Map<String, String> map) {
        mParams.putAll(map);
        return this;
    }
}
